<template>
  <el-dialog :title="obj.isTrue?'角色添加':'角色编辑'" :visible.sync="obj.flag" width="50%"
  @close="kong()">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="角色名称">
        <el-input v-model="form.rolename"></el-input>
      </el-form-item>
      <el-form-item label="角色权限">
        <el-tree
          :data="hh"
          show-checkbox
          node-key="id"
          ref="tree"
          :props="{ children: 'children', label: 'title' }"
        >
        </el-tree>
      </el-form-item>
      <el-form-item label="状态">
        <el-switch
          :active-value="1"
          :inactive-value="2"
          v-model="form.status"
        ></el-switch>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="qu()">取 消</el-button>
      <el-button v-if="obj.isTrue"  @click="add()" type="primary">添加</el-button>
       <el-button v-else @click="update()" type="primary">编辑</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
import { roleadd ,roleInfo,roleEdit} from "../../../request/api.js";
import { successAlert, warningAlert } from "../../../utils/alert.js";
export default {
  props: ["obj"],
  data() {
    return {
      form: {
        rolename: "",
        menus: "",
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({
      hh: "menuList/list",
    }),
  },
  methods: {
    ...mapActions({
      sss: "menuList/asyncChangeList",
      cc:"role/asyncChangeList"

    }),
    //清空并关闭
    kong() {
      this.form = {
        form: {
          rolename: "",
          menus: "",
          status: 1,
        },
      };
      this.$refs.tree.setCheckedKeys([]);
      this.obj.flag = false;
    },
    //添加
    add() {
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      roleadd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
        } else {
          warningAlert(res.data.msg);
        }
      });
      this.kong()
      this.cc()
    },
    qu(){
      this.kong()
    },
    //获取一条信息
    getOne(id){
roleInfo({id:id}).then(res=>{
   this.form = {
          ...res.data.list,
          menus:JSON.parse(res.data.list.menus)
        }
  this.$refs.tree.setCheckedKeys(this.form.menus);
  this.form.id=id
})
    },
    //编辑
    update(){
this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
roleEdit(this.form).then(res=>{
  if(res.data.code==200){
    successAlert(res.data.msg)
    this.kong()
   this.cc()
  }else{
    warningAlert(res.data.msg)
  }
})
    }
  },
  mounted() {
    this.sss();
  },
};
</script>

<style lang="scss" scoped>
</style>